/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package upc.jfactory.scp.jpa.controlador;


import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;

import upc.jfactory.scp.jpa.bean.OfertaEmpresa;
import upc.jfactory.scp.jpa.bean.Postulacion;
import upc.jfactory.scp.jpa.controlador.exceptions.NonexistentEntityException;
import upc.jfactory.scp.jpa.controlador.exceptions.PreexistingEntityException;

/**
 *
 * @author Diego
 */
public class PostulacionJpaController {

    public PostulacionJpaController() {
        emf = Persistence.createEntityManagerFactory("Web-SCPPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Postulacion postulacion) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(postulacion);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findPostulacion(postulacion.getPostulacionId()) != null) {
                throw new PreexistingEntityException("Postulacion " + postulacion + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Postulacion postulacion) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            postulacion = em.merge(postulacion);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = postulacion.getPostulacionId();
                if (findPostulacion(id) == null) {
                    throw new NonexistentEntityException("The postulacion with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Postulacion postulacion;
            try {
                postulacion = em.getReference(Postulacion.class, id);
                postulacion.getPostulacionId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The postulacion with id " + id + " no longer exists.", enfe);
            }
            em.remove(postulacion);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Postulacion> findPostulacionEntities() {
        return findPostulacionEntities(true, -1, -1);
    }

    public List<Postulacion> findPostulacionEntities(int maxResults, int firstResult) {
        return findPostulacionEntities(false, maxResults, firstResult);
    }

    private List<Postulacion> findPostulacionEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Postulacion as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Postulacion findPostulacion(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Postulacion.class, id);
        } finally {
            em.close();
        }
    }

    public int getPostulacionCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Postulacion as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    public List<Postulacion> BuscarPostulacionesxalumno(int id)
     {
         EntityManager em = getEntityManager();
         try
        {
              Query q = em.createNamedQuery("Postulacion.findByPostulanteId");

                q.setParameter("postulanteId",id);
                return q.getResultList();


        }
        finally
        {
            em.close();
        }
      }
    public List<Postulacion> BuscarPostulacionesxoferta(int id)
     {
         EntityManager em = getEntityManager();
         try
        {
              Query q = em.createNamedQuery("Postulacion.findByOfertaempresaId");

                q.setParameter("ofertaempresaId",id);
                return q.getResultList();


        }
        finally
        {
            em.close();
        }
      }
    public List<Postulacion> BuscarPostulacionesDeAlumno(int idalumno, int idofertalaboral)
     {
         EntityManager em = getEntityManager();
         try
        {
              Query q = em.createNamedQuery("Postulacion.findByPostulanteEmpresaId");

                q.setParameter("postulanteId",idalumno);
                q.setParameter("ofertaempresaId",idofertalaboral);
                return q.getResultList();


        }
        finally
        {
            em.close();
        }
      }
    public List<OfertaEmpresa> BuscarEmpresasxalumno(int id)
     {
         EntityManager em = getEntityManager();
         try
        {
              Query q = em.createQuery("SELECT DISTINCT oe.empresa_id FROM Postulacion p, OfertaEmpresa oe WHERE p.ofertaempresa_id=oe.ofertaempresa_id AND p.postulante_id = :postulanteId");

                q.setParameter("postulanteId",id);
                List<Integer> messages= q.getResultList();
                List<OfertaEmpresa> oferts= new ArrayList<OfertaEmpresa>();

                for (Integer s : messages )
                {
                        OfertaEmpresa oemp= new OfertaEmpresa();
                        oemp.setEmpresaId(s);
                        oferts.add(oemp);
                        
                }

                return oferts;
                
        }
        finally
        {
            em.close();
        }
      }



}
